/*
 * $Id: TaskDispatcher.java,v 1.11 2009/02/27 15:27:10 navdeep_mahajan Exp $
 * 
 * Copyright (C) 2008 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.processor.dispatcher;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;

import com.ge.healthcare.autosc.common.database.dao.ITaskClassMapDAO;
import com.ge.healthcare.autosc.common.database.pojo.ActTask;
import com.ge.healthcare.autosc.common.database.pojo.CfgTaskClassMap;
import com.ge.healthcare.autosc.common.util.ApplicationInfo;
import com.ge.healthcare.autosc.processor.PrcCodeConstants;
import com.ge.healthcare.autosc.processor.dispatcher.tasks.ProcessorTask;
import com.ge.healthcare.autosc.processor.exception.TaskDispatcherException;

/**
 * 
 * @author 212042946 The TaskDispatcher dispatches incoming task to task handler
 *         according to the type of task.
 */
public class TaskDispatcher implements InitializingBean {

	private ITaskClassMapDAO iTaskClassMapDAO = null;

	/**
	 * Constructor
	 */
	public TaskDispatcher() {

	}

	/**
	 * Spring Bean cycle method
	 */
	public void afterPropertiesSet() throws Exception {
		Assert
				.notNull(iTaskClassMapDAO,
						"TaskClassMapDAO object is required. Use setTaskClassMapDAO() to provide one.");
	}

	/**
	 * Call task handler to process the task
	 * 
	 * @param actTaskVO
	 * @throws TaskDispatcherException
	 */
	public void processTask(ActTask actTask) throws TaskDispatcherException {

		CfgTaskClassMap taskClassMap = getTaskTypeClassMap(actTask
				.getTaskType());

		ProcessorTask task = (ProcessorTask) ApplicationInfo
				.getApplicationContext().getBean(taskClassMap.getTaskClass());

		task.executeTask(actTask);

	}

	/**
	 * Set TAskClassMapDAO
	 * 
	 * @param taskClassMapDAO
	 */
	public void setTaskClassMapDAO(ITaskClassMapDAO taskClassMapDAO) {
		this.iTaskClassMapDAO = taskClassMapDAO;
	}

	private CfgTaskClassMap getTaskTypeClassMap(String taskType)
			throws TaskDispatcherException {

		CfgTaskClassMap taskClassMap = null;
		taskClassMap = (CfgTaskClassMap) iTaskClassMapDAO
				.getTaskTypeClassMapHash().get(taskType);
		if (taskClassMap == null) {
			throw new TaskDispatcherException(new StringBuffer().append(
					"Cannot dispatch task. ").append(
					"CfgTaskClassMap not found: task type ").append(taskType)
					.toString(), PrcCodeConstants.PRC_F_APP_001);
		}
		return taskClassMap;
	}
}
